<!--********************************************************************
* Copyright© 2000 - 2020 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title data-i18n="resources.title_componentsRankSymbolTheme_React"></title>
    <script type="text/javascript" include="react" src="../js/include-web.js"></script>
    <script include="antd,iclient-mapboxgl-react,mapbox-gl-enhance" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <script type="text/javascript" src="../data/chinaConsumptionLevel.js"></script>
    <style>
      html,
      body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #main {
        margin: 0 auto;
      }
      #main,
      #map {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      .legendItemHeader,
      .legendItemValue {
        width: 120px;
        height: 18px;
        font-size: 14px;
      }
    </style>
  </head>
  <body>
    <div id="main"></div>
    <script type="text/babel">
      var host = window.isLocal ? window.server : 'https://iserver.supermap.io';
      var SmWebMap = SuperMap.Components.SmWebMap;
      var SmRanksymbolThemeLayer = SuperMap.Components.SmRanksymbolThemeLayer;

      var features = [], popup, map;
      for (var i = 0, len = chinaConsumptionLevel.length; i < len; i++) {
        // 省居民消费水平（单位：元）信息
        var provinceInfo = chinaConsumptionLevel[i];
        var geo = new mapboxgl.LngLat(provinceInfo[1], provinceInfo[2]);
        var attrs = {};
        attrs.NAME = provinceInfo[0];
        attrs.CON2009 = provinceInfo[3];
        var fea = new mapboxgl.supermap.ThemeFeature(geo, attrs);
        features.push(fea);
      }

      var mapOptions = {
        container: 'map',
        style: {
          version: 8,
          sources: {
            'raster-tiles': {
              type: 'raster',
              tiles: [host + '/iserver/services/map-china400/rest/maps/China_4326/zxyTileImage.png?z={z}&x={x}&y={y}'],
              tileSize: 256
            }
          },
          layers: [
            {
              id: 'simple-tiles',
              type: 'raster',
              source: 'raster-tiles',
              minzoom: 0,
              maxzoom: 22
            }
          ]
        },
        center: [116.85, 39.79],
        zoom: 3
      };

      var themeOptions = {
        //map: map,//该可选参数将在下个版本遗弃
        themeField: 'CON2009',
        // 配置图表参数
        symbolSetting: {
          //必设参数
          codomain: [0, 40000], // 允许图形展示的值域范围，此范围外的数据将不制作图图形
          //圆最大半径 默认100
          maxR: 100,
          //圆最小半径 默认0
          minR: 0,
          // 圆形样式
          circleStyle: { fillOpacity: 0.8 },
          // 符号专题图填充颜色
          fillColor: '#FFA500',
          // 专题图hover 样式
          circleHoverStyle: { fillOpacity: 1 }
        }
      };
      function showInfoWin(params) {
        var e = params.mapboxEvent;
        map = params.map;
        var themeLayer = map.getLayer(params.layerId);
        // e.target 是图形对象，即数据的可视化对象。
        // 图形对象的 refDataID 属性是数据（feature）的 id 属性，它指明图形对象是由那个数据制作而来;
        // 图形对象的 dataInfo 属性是图形对象表示的具体数据，他有两个属性，field、R 和 value;
        if (e.target && e.target.refDataID && e.target.dataInfo) {
          closeInfoWin();
          // 获取图形对应的数据 (feature)
          var fea = themeLayer.getFeatureById(e.target.refDataID);
          var info = e.target.dataInfo;
          // 弹窗内容
          var contentHTML = "<div style='color: #000; background-color: #fff'>";
          contentHTML += resources.text_Name + '<br><strong>' + fea.attributes.NAME + '</strong>';
          contentHTML += "<hr style='margin: 3px'>";
          switch (info.field) {
            case 'CON2009':
              contentHTML +=
                resources.text_consumptionLevel1 +
                '09' +
                resources.text_consumptionLevel2 +
                ' <br/><strong>' +
                info.value +
                '</strong>（' +
                resources.text_yuan +
                '）';
              break;
            default:
              contentHTML += 'No Data';
          }
          contentHTML += '</div>';
          var tempPoint = map.unproject(new window.mapboxgl.Point(e.event.x, e.event.y));
          popup = new mapboxgl.Popup({ closeOnClick: false })
            .setLngLat([tempPoint.lng, tempPoint.lat])
            .setHTML(contentHTML)
            .addTo(map);
          return;
        }
        closeInfoWin();
      }
      function closeInfoWin() {
        if (popup) {
          popup.remove(map);
        }
      }

      ReactDOM.render(
        <SmWebMap mapOptions={mapOptions}>
          <SmRanksymbolThemeLayer
            options={themeOptions}
            layerName={'RankSymbolThemeLayer'}
            data={features}
            chartsType={'Circle'}
            onMousemove={showInfoWin}
          />
        </SmWebMap>,
        document.getElementById('main')
      );
    </script>
  </body>
</html>
